pp108 : Process Platform Formatting Plugin

Process Platform Formatting Plugin

The cordys.formatting plugin is used to format the date, number, and currency fields according to the user preferences. It is part of the HTML5SDK Utils. If you want to use this plugin, you must include it in the code.

Methods

The following methods are supported by the cordys.formatting plugin:

Table 1. List of Methods

Method

Description

formatDecimal(decimal)

Returns the formatted decimal value for the provided decimal

formatNumber(number) Returns the formatted number value for the provided number
formatCurrency(value) Returns the formatted currency value for the provided value
formatDate(date,pattern) Returns the formatted date value in the provided pattern. Refer to the date tokens section for more information.
formatDateTime(value,pattern) Returns the formatted date time in the provided pattern. Refer to the date tokens section for more information.

Date Tokens

Following are the Date parsing tokens, which can be used to form the date pattern:

Table 2. Date tokens

Token Output
Month M Month number. For example, 1 2 ... 11 12
MM Month number in double digits. For example, 01 02 ... 11 12
MMM Abbreviated month names.For example, Jan Feb ... Nov Dec
MMMM Month name. For example, January February ... November December
Day of Month D Day of month. For example, 1 2 ... 30 31
DD Day of month in double digits. For example, 01 02 ... 30 31
Day of Year DDD Day of year. For example, 1 2 ... 364 365
DDDD Day of year in three digits. For example, 001 002 ... 364 365
Day of Week ddd Abbreviated week names. For example, Sun Mon ... Fri Sat
dddd Week names. For example, Sunday Monday ... Friday Saturday
Year YY Double digit year. For example, 70 71 ... 29 30
YYYY Four digit year. For example, 1970 1971 ... 2029 2030
Hour H Single digit 24 hour time. For example, 0 1 ... 22 23
HH Single digit 24 hour time. For example, 00 01 ... 22 23
h Single digit 12 hour time. For example, 1 2 ... 11 12
hh Double digit 12 hour time. For example, 01 02 ... 11 12
Minute m Single digit minutes. For example, 0 1 ... 58 59
mm Double digit minutes.For example, 00 01 ... 58 59
Second s Single digit seconds. For example, 0 1 ... 58 59
ss Double digit seconds. For example, 00 01 ... 58 59
SSS Milliseconds (1/1000th of a second)
Localized Formats LT Time. For example, 8:30 PM
L Short date. For example, 09/04/1986
LL Long date. For example, September 4 1986

Following are the few examples for the date patterns:

Sample Patterns
YYYY-MM-DD
MM-DD-YYYY
YYYY-MM-DD HH
YYYY-MM-DD HH:mm 
YYYY-MM-DD HH:mm:ss 
YYYY-MM-DD HH:mm:ss.SSS
YYYY-MM-DDTHH:mm:ss.SSS 

Default Properties

Some properties have a default value set by the cordys.formatting plug-in:

Table 3. Default Properties

Property

Default value

dateTimePattern

YYYY-MM-DD HH:mm:ss

datePattern

YYYY-MM-DD

You can modify the default properties by using the object $.cordys.formatting.defaults. For example, to set the date pattern of all the date fields in your page to "MM/DD/YYYY", do the following:

$.cordys.formatting.defaults.datePattern= "MM/DD/YYYY"; 

Knockout Binding

Process Platform formatting plugin also provides a set of knockout custom bindings for binding in respective formats. The custom bindings provided are as follows:

Table 1. List of bindings

Binding

Description

number

The number binding causes the associated DOM element to display the formatted number value of your parameter.

decimal The decimal binding causes the associated DOM element to display the formatted decimal value of your parameter.
currency The currency binding causes the associated DOM element to display the formatted currency value of your parameter.
date

The date binding causes the associated DOM element to display the formatted date value of your parameter.
Pattern can be provided in the data-datepattern attribute of the DOM element. If it is not provided, it will be formatted based on the default date pattern.

datetime

The datetime binding causes the associated DOM element to display the formatted datetime value of your parameter.
Pattern can be provided in the data-datepattern attribute of the DOM element. If it is not provided, it will be formatted based on the default datetime pattern.

Examples

The following are the sample code snippets:

Get Formatted number
$.cordys.formatting.formatNumber(salary).done(function(formattedValue){ // do something with the formatted number value })
Get Formatted date
$.cordys.formatting.formatDate(expiryDate,"DD/MM/YYYY").done(function(formattedDateValue){ // do something with the formatted date value }) 
Number binding
<input data-bind="number:salary" id="fldSalary" type="text"/>
Decimal binding
<input id="fldAmount" data-bind="decimal:Amount" type="text" />  
Currency binding
<input data-bind="currency:price" id="fldPrice" type="text"/>
Date binding
<input data-bind="datetime:expiryData" data-datepattern="DD-MM-YYYY" id="fldExpiryDate"/>
Date Time binding
<input data-bind="date:createdDate"
    data-datepattern="DD-MM-YYYY HH:mm:ss a" id="fldCreataedDate"/>